package com.deke.home.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deke.entity.AclPermission;
import com.deke.entity.TbBottomArticle;
import com.deke.entity.TbHeadNav;
import com.deke.home.dao.TbBottomArticleDao;
import com.deke.home.service.TbBottomArticleService;
import com.deke.home.vo.TbBottomArticleVo;
import com.deke.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ：ludi（206913808@qq.com）
 * @date ：Created in 2022/12/9 15:54
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class TbBottomArticleServiceImpl implements TbBottomArticleService {

    @Resource
    private TbBottomArticleDao tbBottomArticleDao;

    @Override
    public Result queryByConditionPage(Integer current, Integer pageSize, TbBottomArticleVo tbBottomArticleVo) {
        IPage<TbBottomArticle> page = new Page(current,pageSize);
        QueryWrapper<TbBottomArticle> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(tbBottomArticleVo.getId())){
            wrapper.like("id",tbBottomArticleVo.getId());
        }
        if (StringUtils.hasText(tbBottomArticleVo.getName())){
            wrapper.like("name", tbBottomArticleVo.getName());
        }
        if (!StringUtils.isEmpty(tbBottomArticleVo.getIsDisable())){
            wrapper.eq("is_disable", tbBottomArticleVo.getIsDisable());
        }

        List<TbBottomArticle> list = tbBottomArticleDao.queryAll();
        List<TbBottomArticle> first = new ArrayList<>();
        String a = "0";
        for (TbBottomArticle tbBottomArticle : list) {
            if (tbBottomArticle.getParentId().equals(a)){
                first.add(tbBottomArticle);
            }
        }
        for (TbBottomArticle f : first) {
            f.setChildren(findChildren(list,f.getId()));
        }

        Map<String,Object> map = new HashMap<>();


        IPage<TbBottomArticle> iPage = tbBottomArticleDao.selectPage(page, wrapper);
        map.put("first", first);
        map.put("iPage", iPage);
        return new Result(2000,"查询成功",map);

    }
    
    /**
     * @create by: Username 刘亦辰 
     * @description: 前台 底部文章查询
     * @create time: 2022/12/21 17:11
     * @param null 
     * @return :  
     **/
    @Override
    public Result getQianAllBottomArticle() {
        List<TbBottomArticle> first = new ArrayList<>();
        QueryWrapper<TbBottomArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("is_disable",0);
        List<TbBottomArticle> bottomArticleList = tbBottomArticleDao.selectList(wrapper);
        if (ObjectUtil.isNotEmpty(bottomArticleList) && bottomArticleList.size()>0){
            String a = "0";
            for (TbBottomArticle tbBottomArticle : bottomArticleList) {
                if (a.equals(tbBottomArticle.getParentId())){
                    first.add(tbBottomArticle);
                }
            }
            for (TbBottomArticle f : first) {
                f.setChildren(findChildren(bottomArticleList,f.getId()));
            }
            return new Result(2000,"成功",first);
        }
        return new Result(5000,"失败");
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 递归查询
     * @create time: 2022/12/9 16:39
     * @param list
     * @param id
     * @return java.util.List<com.deke.entity.TbBottomArticle>
     */
    private List<TbBottomArticle> findChildren(List<TbBottomArticle> list, String id) {
        List<TbBottomArticle> seconds = new ArrayList<>();
        for (TbBottomArticle list1 : list) {
            if (list1.getParentId().equals(id)){
                seconds.add(list1);
            }
        }
        for (TbBottomArticle s : seconds) {
            s.setChildren(findChildren(list,s.getId()));
        }
        return seconds;
    }

    /**
     * @param map
     * @return com.deke.vo.Result
     * @create by: ludi（206913808@qq.com）
     * @description: 删除操作
     * @create time: 2022/12/12 15:35
     */
    @Override
    public Result deleteBottomArticle(Map map) {
        UpdateWrapper<TbBottomArticle> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", map.get("id"));
        TbBottomArticle tbBottomArticle = new TbBottomArticle();
        tbBottomArticle.setIsDeleted(1);
        int update = tbBottomArticleDao.update(tbBottomArticle, wrapper);
        if (update == 1) {
            return new Result(2000, "删除成功");
        } else {
            return new Result(5000, "删除失败");
        }
    }

    @Override
    public Result addBottomArticle(TbBottomArticle tbBottomArticle) {
        tbBottomArticle.setIsDisable(0);
        tbBottomArticle.setName(tbBottomArticle.getName());
        tbBottomArticle.setIsDeleted(0);
        tbBottomArticle.setGmtCreate(new Date());
        tbBottomArticle.setGmtModified(new Date());
        tbBottomArticleDao.insert(tbBottomArticle);
        return new Result(2000, "添加成功");
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 改变状态
     * @create time: 2022/12/17 15:19
     * @param id
     * @param isDisable
     * @return com.deke.vo.Result
     */
    @Override
    public Result changeStatus(String id, Integer isDisable) {
        UpdateWrapper<TbBottomArticle> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        TbBottomArticle tbBottomArticle = new TbBottomArticle();
        tbBottomArticle.setIsDisable(isDisable);
        tbBottomArticleDao.update(tbBottomArticle, wrapper);
        return new Result(2000, "状态改变成功");
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 修改操作
     * @create time: 2022/12/17 15:24
     * @param name
     * @return com.deke.vo.Result
     */
    @Override
    public Result editBottomArticle(TbBottomArticle tbBottomArticle) {
        UpdateWrapper<TbBottomArticle> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", tbBottomArticle.getId());
        tbBottomArticle.setName(tbBottomArticle.getName());
        tbBottomArticleDao.update(tbBottomArticle,wrapper);
        return new Result(2000, "修改成功");
    }



}